<template>
  <div class="search">
    <p
      class="info"
      v-text="`Found ${list.total} book contain “${$route.params.keywords}”`"
    ></p>
    <main>
      <ul class="info-list">
        <list-template
          v-for="item of list.rows"
          :key="item.bookId"
          :item="item"
          :diff="'search-list'"
        />
      </ul>
      <div class="page-wrap">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page="params.pageNum"
          :page-size="params.pageSize"
          hide-on-single-page
          background
          layout="prev, pager, next"
          :total="list.total"
        >
        </el-pagination>
      </div>
    </main>
    <div v-show="isEmpty" class="empty">
      <svg-icon class="empty-icon" icon-class="empty-search"></svg-icon>
      <p>No resources available</p>
    </div>
  </div>
</template>

<script>
import listTemplate from "@/components/common/listTemplate/Index";
export default {
  data() {
    return {
      list: {}, //书籍列表
      params: {
        pageNum: 1,
        pageSize: 10,
        content: this.$route.params.keywords,
      },
      scrollTop: 0,
      isEmpty: false,
    };
  },
  components: {
    listTemplate,
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    this.search();
  },
  methods: {
    //当前页
    handleCurrentChange(val) {
      this.params.pageNum = val;
      this.search();
      this.backTop();
    },
    async search() {
      try {
        let res = await this.$api.home.search(this.params);
        if (res.code === 1) {
          this.list = res.source;
          if (res.source.total > 0) {
            this.isEmpty = false;
          } else {
            this.isEmpty = true;
          }
        }
      } catch (e) {
        console.log(e);
      }
    },
    // 返回顶部
    backTop() {
      let timer = setInterval(() => {
        let ispeed = Math.floor(-this.scrollTop / 5);
        document.documentElement.scrollTop = document.body.scrollTop =
          this.scrollTop + ispeed;
        if (this.scrollTop === 0) {
          clearInterval(timer);
        }
      }, 50);
    },
  },
};
</script>

<style lang="scss" scoped>
.search {
  width: 1200px;
  margin: 48px auto;
  > .info {
    line-height: 43px;
    font-size: 26px;
    color: #333333;
    font-family: Poppins-SemiBold, Poppins;
    font-weight: 600;
  }
  main {
    .info-list {
      margin-top: 48px;
    }
  }
  > .empty {
    text-align: center;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .empty-icon {
      width: 276px;
      height: 195px;
    }
    > p {
      margin-top: 41px;
      font-size: 14px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      color: #999999;
      line-height: 20px;
      .toPage {
        cursor: pointer;
        text-decoration: underline;
        color: #e86472;
      }
    }
  }
}
</style>
